import io
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
from PIL import ImageTk, Image
from config.Instrument import Instrument_img
from config.Instrument import Instrument_toml
from middleWare import Middleware
from dao.Result import Result

class User(Frame):
    def __init__(self, master):
        self.master = master
        self.main_color = "#ffffff"
        self.mid = Middleware()
        self.result = Result(**self.get_config())
    
    def get_config(self):
        toml = Instrument_toml()
        user = toml.GetStrogeToml("user")
        password = toml.GetStrogeToml("password")
        database = toml.GetStrogeToml("database")
        url = toml.GetStrogeToml("url")

        return {
            "url": url,
            "user": user,
            "password": password,
            "db": database
        }

        
    def window(self):
        self.master.title("User")
        self.master.geometry("600x400")
        #设置图标
        self.master.iconbitmap("static/favicon.ico")
        # self.master.configure(bg="#fafafa")
        self.width = self.master.winfo_screenwidth()  
        self.height = self.master.winfo_screenheight()  
        self.x = (self.width - 600) // 2  
        self.y = (self.height - 400) // 2   
        self.master.geometry("{}x{}+{}+{}".format(600, 400, self.x, self.y)) 

        #设置一个文本域，文本为用户设置
        self.text = Label(self.master, text="用户设置", font=("微软雅黑", 15),fg="black")
        self.text.place(x=20, y=10)

        #设置一个文本域，文本为：“”
        self.text = Label(self.master, text=" ", bg=self.main_color, font=("微软雅黑", 20),fg="black")
        #配置宽高
        self.text.configure(width=35, height=9)
        self.text.place(x=20, y=70)


        img = Instrument_img("static/白色建筑.png")
        img.resize_img(100,100)
        mage = img.get_img()
        global img_t
        img_t =ImageTk.PhotoImage(mage)
        # 画一个圆
        self.circle = Canvas(self.master, width=110, height=110, bg='#3b80f9', bd=0, highlightthickness=0)
        self.circle.place(x=30, y=120)
        self.circle.create_oval(0, 0, 100, 100, fill='#3b80f9')
        #设置没有背景色
        self.circle.configure(background='#ffffff')
        #设置圆范围内为白色
        self.circle.create_oval(0, 0, 100, 100, fill='#e6e6e6',outline='#ffffff')
        self.circle.create_image(0,0,anchor=NW,image=img_t)


        #设置一个文本域，文本为：“你的账户配置”
        self.text = Label(self.master, text="账户配置", bg=self.main_color, font=("宋体", 12),fg="black")
        self.text.place(x=260, y=80)
        
        #画一条线
        self.line = Canvas(self.master, width=566, height=2, bg="#e6e6e6", bd=0, highlightthickness=0)
        self.line.place(x=20, y=110)

        #设置一个文本域，内容为：”基础属性“
        self.text = Label(self.master, text="基础属性", bg=self.main_color, font=("宋体", 12),fg="black")
        self.text.place(x=200, y=120)

        #设置一个文本域，内容为：”用户名“
        self.text = Label(self.master, text="用户名", bg=self.main_color, font=("宋体", 8),fg="black")
        self.text.place(x=200, y=150)

        #设置一个文本域，内容为：”昵称“
        self.text = Label(self.master, text="签名", bg=self.main_color, font=("宋体", 8),fg="black")
        self.text.place(x=380, y=150)

        #设置一个输入框，默认输入：”SiYuan“
        self.text_u = Entry(self.master, width=20, font=("宋体", 8),fg="black",bg="#fafafa")
        self.text_u.place(x=200, y=180)
        self.text_u.insert(0, "SiYuan")

        

        #设置一个输入框，内容为：”“
        self.text_use = Entry(self.master, bg="#fafafa", font=("宋体", 8),fg="black")
        self.text_use.place(x=380, y=180)
        self.text_use.insert(0, "我想不上班也有钱")

        #设置一个文本域，内容为：密码
        self.text = Label(self.master, text="密码", bg=self.main_color, font=("宋体", 8),fg="black")
        self.text.place(x=200, y=260)

        #设置一个输入框，默认输入：”"
        self.text2 = Entry(self.master, width=20, font=("宋体", 8),fg="black",bg="#fafafa")
        self.text2.place(x=200, y=290)
        #设置密码隐藏
        self.text2.configure(show="*")
        self.text2.insert(0, "******")

        #设置一个文本域，文本内容为确认密码
        self.text = Label(self.master, text="确认密码", bg=self.main_color, font=("宋体", 8),fg="black")
        self.text.place(x=380, y=260)
        #设置密码隐藏
        
        #设置一个输入框，文本内容为 ”“
        self.text_p = Entry(self.master, width=20, font=("宋体", 8),fg="black",bg="#fafafa")
        self.text_p.place(x=380, y=290)
        self.text_p.insert(0, "******")
        #设置密码隐藏
        self.text_p.configure(show="*")

        #设置一个文本域，文本内容为：”修改密码“
        self.text = Label(self.master, text="修改密码", bg=self.main_color, font=("宋体", 12),fg="black")
        self.text.place(x=200, y=220)

        self.get_user()

        #设置一个按钮，颜色为渐变色
        self.button = Button(self.master, text="确认", bg="#3b80f9", fg="white", font=("宋体", 9),width=12, height=3, activebackground="#3b80f9", activeforeground="white", bd=0, highlightthickness=0)
        self.button.place(x=200, y=340)
        self.button.configure(command=self.changeinfo)

        #读取照片为按钮
        img_b = Instrument_img("static/替换.png")
        img_b.resize_img(30,30)
        mage_b = img_b.get_img()
        global img_b_t
        img_b_t =ImageTk.PhotoImage(mage_b)
        #设置一个新按钮
        self.button1 = Button(self.master, text=" ", bg="#ffffff", fg="black", font=("宋体", 9),width=30, height=30, activebackground="#fafafa", activeforeground="black", bd=0, highlightthickness=0,image=img_b_t)
        self.button1.configure(command=self.change_head)
        self.button1.place(x=55, y=230)

        #设置一个文本域
        self.text3 = Label(self.master, text="切换", bg=self.main_color, font=("宋体", 8),fg="black")
        self.text3.place(x=85, y=237)

    def change_head(self):
        file_path = filedialog.askopenfilename(filetypes=[("Image files", "*.jpg;*.png")])
        if file_path != "":
            img = Instrument_img(file_path)
            img.resize_img(100,100)
            mage = img.get_img()
            global img_t
            img_t =ImageTk.PhotoImage(mage)
            self.circle.create_image(0,0,anchor=NW,image=img_t)
            
            with open(file_path, 'rb') as f:
                urls = f.read()

            # print(urls)
            ans = self.mid.data_binding_user_by_id(self.user)
            uid = ans['user_id']
            self.result.start()
            self.result.update_head(urls,uid)
            self.result.close()
    
    def get_user(self):
        inst = Instrument_toml()
        self.user = inst.GetStrogeToml("nickname")
        
        ans = self.mid.data_binding_user_by_id(self.user)
        self.text_use.delete(0, END)
        self.text_use.insert(0,ans['user_desc'])

        self.text2.delete(0, END)
        self.text2.insert(0,ans['password'])
        
        self.text_p.delete(0, END)
        self.text_p.insert(0,ans['password'])

        #清空输入框
        self.text_u.delete(0, END)
        #插入用户名
        self.text_u.insert(0,self.user)

        #配置图片
        try:
            image = Instrument_img(io.BytesIO(ans['head']))
            image.resize_img(100,100)
            mage_t = image.get_img()
            global img_t
            img_t =ImageTk.PhotoImage(mage_t)
            self.circle.create_image(0,0,anchor=NW,image=img_t)
        except:
            pass

    #修改toml配置文件
    def change_toml(self,nickname):
        inst = Instrument_toml()
        inst.SetStrogeToml("nickname",nickname)

    def changeinfo(self):
        nickname = self.text_u.get()
        user_desc = self.text_use.get()
        password = self.text2.get()
        password_1 = self.text_p.get()
        if password != password_1:
            messagebox.showinfo("错误", "两次输入密码不一致")
        else:
            temp = self.mid.contian(nickname)
            if nickname != self.user:
                if temp:
                    messagebox.showinfo("错误", "昵称已存在")
                else:
                    ans = self.mid.data_binding_user_by_id(self.user)
                    uid = ans['user_id']
                    self.result.start()
                    self.result.update(uid,user_desc=user_desc,password=password,nickname=nickname)
                    self.result.close()
                    self.change_toml(self.user)
                    messagebox.showinfo("成功", "修改成功")
            else:
                ans = self.mid.data_binding_user_by_id(self.user)
                uid = ans['user_id']
                self.result.start()
                self.result.update(uid,user_desc=user_desc,password=password,nickname=nickname)
                self.result.close()
                self.change_toml(self.user)
                messagebox.showinfo("成功", "修改成功")

    def start(self):
        self.window()
        self.master.mainloop()

# if __name__ == '__main__':
#     window = Tk()
#     user = User(window)
#     user.start()